package com.fansl.allround.study.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author fansl
 * @Description: TODO
 * @date 2019/12/12 18:12
 */
@Component
@Slf4j
public class AliSmsSendHandler {

    private ThreadLocal<SmsConfig> smsConfig = ThreadLocal.withInitial(()-> new SmsConfig("初始"));

    private ThreadLocal<SmsService> smsService = ThreadLocal.withInitial(()-> new SmsService());

    public SmsConfig getSmsConfig() {
        return smsConfig.get();
    }

    public void setSmsConfig(SmsConfig smsConfig) {
        this.smsConfig.set(smsConfig);
    }

    public SmsService getSmsService() {
        return smsService.get();
    }

    public void setSmsService(SmsService smsService) {
        this.smsService.set(smsService);
    }

    public void send(String uuid, String message){
        try {
            log.info(uuid+","+"发送消息："+message);
            log.info(uuid+","+"SmsService:"+smsService.get().getServiceName());
            log.info(uuid+","+"SmsConfig:"+smsConfig.get().getConfigName());
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            log.error("{}",uuid,e);
        }finally {
            clearThreadLocal();
            log.info("{},移除ThreadLocalInfo",uuid);
        }
    }

    public void clearThreadLocal(){
        smsService.remove();
        smsConfig.remove();
    }
}
